//
//  ViewController.m
//  RunTime
//
//  Created by 张飞跃 on 16/5/27.
//  Copyright © 2016年 冰泪. All rights reserved.
//

#import "ViewController.h"
#import <objc/runtime.h>
#import <objc/message.h>
#import "UIView+ZFYType.h"
#import "ViewTypeString.h"
#import "UIView+ZFYTagString.h"

static void *alertViewKey = "alertViewKey";

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIView *v= [[UIView alloc]initWithFrame:CGRectMake(0, 100, 100, 100)];
    v.backgroundColor = [UIColor redColor];
    v.tagString = @"111"; //view 添加字符串tag  类型
    [self.view addSubview:v];
    
    NSLog(@"%@",v.tagString);
//    UIView *a=[self.view viewWithTagString:@"111"];
//    a.backgroundColor = [UIColor blueColor];
    
    
    ViewTypeString *model = [[ViewTypeString alloc]init];
    model.type = @"aaaa";
    model.name = @"bbb";
    
    v.model = model;   //view 添加字符串model数据类型

    NSLog(@"%@",v.model);
    
    
    
    
//    NSString * htmlString = [NSString stringWithFormat:@"计划时长:1天 <a href=\"http://www.baidu.com\">百度</a> 计划时长 <a href=\"http://www.baidu.com\">谷歌</a>"];
//    
//    NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUnicodeStringEncoding] options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType} documentAttributes:nil error:nil];
//     label.attributedText = attrStr;
//    NSArray *arr = [[NSArray alloc]initWithArray:[self allProperties]];
//    NSLog(@"%@",arr);
//    
//    NSDictionary *dic = [self allPropertyNamesAndValues];
//    NSLog(@"dic--%@",dic);
//    for (NSString *propertyName in dic.allKeys){
//        NSLog(@"propertyName: %@ propertyValue: %@", propertyName, dic[propertyName]);
//    }
}
//#pragma mark  获取类的所有属性
//- (NSArray *)allProperties {
//    unsigned int count;
//    
//    // 获取类的所有属性
//    // 如果没有属性，则count为0，properties为nil
//    objc_property_t *properties = class_copyPropertyList([ViewTypeString class], &count);
//    NSMutableArray *propertiesArray = [NSMutableArray arrayWithCapacity:count];
//    
//    for (NSUInteger i = 0; i < count; i++) {
//        // 获取属性名称
//        const char *propertyName = property_getName(properties[i]);
//        NSString *name = [NSString stringWithUTF8String:propertyName];
//        
//        [propertiesArray addObject:name];
//    }
//    
//    // 注意，这里properties是一个数组指针，是C的语法，
//    // 我们需要使用free函数来释放内存，否则会造成内存泄露
//    free(properties);
//    
//    return propertiesArray;
//}
//#pragma mark 获取对象的所有属性名和属性值
//- (NSDictionary *)allPropertyNamesAndValues {
//    ViewTypeString *aa = [[ViewTypeString alloc]init];
//    aa.type = @"1a";
//    aa.name = @"2aa";
//    NSString *ss =  objc_msgSend(aa, @selector(aaa));
//    
//    NSLog(@"%@",ss);
//    NSMutableDictionary *resultDict = [NSMutableDictionary dictionary];
//    unsigned int outCount;
//    objc_property_t *properties = class_copyPropertyList([ViewTypeString class], &outCount);
//    for (int i = 0; i < outCount; i++) {
//        objc_property_t property = properties[i];
//        const char *type = property_getName(property);
//        // 得到属性名
//        NSString *propertyName = [NSString stringWithUTF8String:type];
//        // 获取属性值
//        id propertyValue = [aa valueForKey:propertyName];
//        if (propertyValue && propertyValue != nil) {
//            [resultDict setObject:propertyValue forKey:propertyName];
//        }
//    }
//    // 记得释放
//    free(properties);
//    
//    
//    return resultDict;
//}

- (IBAction)btnClick:(UIButton *)sender {
    
    UILabel *label1=(UILabel*)[self.view viewWithTagString:@"label"];
    label1.text =label1.tagString;
}




- (IBAction)showAlertOne:(id)sender {
    UIAlertView *alertOne = [[UIAlertView alloc]initWithTitle:@"AlertOne" message:@"" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    void (^block)(NSInteger) = ^(NSInteger buttonIndex) {
        if (buttonIndex == 1) {
            NSLog(@"111");
        }
    };
    
    objc_setAssociatedObject(alertOne, alertViewKey, block, OBJC_ASSOCIATION_COPY);//将block 与 alertOne 关联
    [alertOne show];
}
- (IBAction)showAlertTwo:(id)sender {
    UIAlertView *alertTwo = [[UIAlertView alloc]initWithTitle:@"AlertTwo" message:@"" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    void (^block)(NSInteger) = ^(NSInteger buttonIndex) {
        if (buttonIndex == 1) {
             NSLog(@"222");
        }
    };
    objc_setAssociatedObject(alertTwo, alertViewKey, block, OBJC_ASSOCIATION_COPY);
    [alertTwo show];
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0) {
        return;
    }
    void(^block)(NSInteger) = objc_getAssociatedObject(alertView, alertViewKey);
    block(buttonIndex);
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
